﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Itfort.Web
{
	/// <summary>
	/// HttpRequest扩展方法。
	/// </summary>
	public static class HttpRequestExtensions
	{
		/// <summary>
		/// 从Request中返回参数值转化成String
		/// </summary>
		/// <param name="paramName">参数名</param>
		/// <returns></returns>
		public static String ToString(String paramName)
		{
			return ToString(paramName, "");
		}

		/// <summary>
		/// 从Request中返回参数值转化成String
		/// </summary>
		/// <param name="paramName">参数名</param>
		/// <param name="defaultValue">默认值</param>
		/// <returns></returns>
		public static string ToString(string paramName, string defaultValue)
		{
			string qs = HttpContext.Current.Request[paramName];
			if (qs == null)
			{
				return defaultValue;
			}
			return qs.Trim();
		}

		/// <summary>
		/// 从Request中返回参数值转化成String
		/// </summary>
		/// <param name="paramName">参数名</param>
		/// <returns></returns>
		public static String FormToString(String paramName)
		{
			if (String.IsNullOrEmpty(HttpContext.Current.Request.Form[paramName]))
			{
				return "";
			}

			return HttpContext.Current.Request.Form[paramName];
		}

		/// <summary>
		/// 将从Request中返回的参数值转化String数组。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static string[] ToArray(this HttpRequest request, string paramName)
		{
			string qs_value = request[paramName];

			if (!String.IsNullOrEmpty(qs_value))
			{
				String[] tempArray = qs_value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
				return tempArray;
			}
			else
			{
				return null;
			}
		}

		#region Int32

		/// <summary>
		/// 将从Request中返回的参数值转化成Int32
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static int ToInt32(this HttpRequest request, string paramName)
		{
			return request.ToInt32(paramName, 0);
		}

		/// <summary>
		/// 将从Request中返回的参数值转化成Int32
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <param name="defaultValue">默认值</param>
		/// <returns></returns>
		public static int ToInt32(this HttpRequest request, string paramName, int defaultValue)
		{
			string paramValue = request[paramName];
			int returnValue;
			if (String.IsNullOrEmpty(paramValue) || !Int32.TryParse(paramValue, out returnValue))
			{
				returnValue = defaultValue;
			}
			return returnValue;
		}

		/// <summary>
		/// 将从Request中返回的参数值转化成Int32列表。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static List<int> ToInt32List(this HttpRequest request, string paramName)
		{
			string[] tempArray = request.ToArray(paramName);
			if (tempArray == null) return new List<int>();
			return tempArray.Select(p => Int32.Parse(p)).ToList();
		}

		/// <summary>
		/// 将从Request中返回的参数值转化Int32数组。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static int[] ToInt32Array(this HttpRequest request, string paramName)
		{
			return request.ToInt32List(paramName).ToArray();
		}

		#endregion

		#region Int64

		/// <summary>
		/// 将从Request中返回的参数值转化成Int64
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static long ToInt64(this HttpRequest request, String paramName)
		{
			return request.ToInt64(paramName, 0);
		}

		/// <summary>
		/// 将从Request中返回的参数值转化成Int64
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <param name="defaultValue">默认值</param>
		/// <returns></returns>
		public static long ToInt64(this HttpRequest request, String paramName, long defaultValue)
		{
			string paramValue = request[paramName];
			long returnValue = 0;
			if (String.IsNullOrEmpty(paramValue) || !Int64.TryParse(paramValue, out returnValue))
			{
				returnValue = defaultValue;
			}

			return returnValue;
		}

		/// <summary>
		/// 将从Request中返回的参数值转化成Int64列表。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static List<long> ToInt64List(this HttpRequest request, string paramName)
		{
			string[] tempArray = request.ToArray(paramName);
			if (tempArray == null) return new List<long>();
			return tempArray.Select(p => Int64.Parse(p)).ToList();
		}

		/// <summary>
		/// 将从Request中返回的参数值转化Int64数组。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static long[] ToInt64Array(this HttpRequest request, string paramName)
		{
			return request.ToInt64List(paramName).ToArray();
		}

		#endregion

		#region Double

		/// <summary>
		/// 从Request中返回参数值转化成int
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static double ToDouble(this HttpRequest request, string paramName)
		{
			return request.ToDouble(paramName, 0);
		}

		/// <summary>
		/// 将从Request中返回的参数值转化成Double。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <param name="defaultValue">默认值</param>
		/// <returns></returns>
		public static double ToDouble(this HttpRequest request, string paramName, double defaultValue)
		{
			string paramValue = request[paramName];
			double returnValue;
			if (String.IsNullOrEmpty(paramValue) || !Double.TryParse(paramValue, out returnValue))
			{
				returnValue = defaultValue;
			}
			return returnValue;
		}

		/// <summary>
		/// 将从Request中返回的参数值转化成Double列表。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static List<double> ToDoubleList(this HttpRequest request, string paramName)
		{
			string[] tempArray = request.ToArray(paramName);
			if (tempArray == null) return new List<double>();
			return tempArray.Select(p => Double.Parse(p)).ToList();
		}

		/// <summary>
		/// 将从Request中返回的参数值转化Double数组。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static double[] ToDoubleArray(this HttpRequest request, string paramName)
		{
			return request.ToDoubleList(paramName).ToArray();
		}

		#endregion

		#region Decimal

		/// <summary>
		/// 从Request中返回参数值转化成int
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static decimal ToDecimal(this HttpRequest request, string paramName)
		{
			return request.ToDecimal(paramName, 0);
		}

		/// <summary>
		/// 将从Request中返回的参数值转化成Decimal。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <param name="defaultValue">默认值</param>
		/// <returns></returns>
		public static decimal ToDecimal(this HttpRequest request, string paramName, decimal defaultValue)
		{
			string paramValue = request[paramName];
			decimal returnValue;
			if (String.IsNullOrEmpty(paramValue) || !Decimal.TryParse(paramValue, out returnValue))
			{
				returnValue = defaultValue;
			}
			return returnValue;
		}

		/// <summary>
		/// 将从Request中返回的参数值转化成Decimal列表。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static List<decimal> ToDecimalList(this HttpRequest request, string paramName)
		{
			string[] tempArray = request.ToArray(paramName);
			if (tempArray == null) return new List<decimal>();
			return tempArray.Select(p => Decimal.Parse(p)).ToList();
		}

		/// <summary>
		/// 将从Request中返回的参数值转化Decimal数组。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static decimal[] ToDecimalArray(this HttpRequest request, string paramName)
		{
			return request.ToDecimalList(paramName).ToArray();
		}

		#endregion

		#region Boolean

		/// <summary>
		/// 将从Request中返回的参数值转化成Boolean
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static bool ToBoolean(this HttpRequest request, string paramName)
		{
			return request.ToBoolean(paramName, false);
		}

		/// <summary>
		/// 将从Request中返回的参数值转化成Boolean。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <param name="defaultValue">默认值</param>
		/// <returns></returns>
		public static bool ToBoolean(this HttpRequest request, string paramName, Boolean defaultValue)
		{
			string paramValue = request[paramName];
			bool returnValue;
			if (String.IsNullOrEmpty(paramValue) || !Boolean.TryParse(paramValue, out returnValue))
			{
				returnValue = defaultValue;
			}
			return returnValue;
		}

		#endregion

		#region DateTime

		/// <summary>
		/// 将从Request中返回的参数值转化成DateTime
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static DateTime ToDateTime(this HttpRequest request, string paramName)
		{
			return request.ToDateTime(paramName, DateTime.MinValue);
		}

		/// <summary>
		/// 将从Request中返回的参数值转化成DateTime。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <param name="defaultValue">默认值</param>
		/// <returns></returns>
		public static DateTime ToDateTime(this HttpRequest request, string paramName, DateTime defaultValue)
		{
			string paramValue = request[paramName];
			DateTime returnValue;
			if (String.IsNullOrEmpty(paramValue) || !DateTime.TryParse(paramValue, out returnValue))
			{
				returnValue = defaultValue;
			}
			return returnValue;
		}

		/// <summary>
		/// 将从Request中返回的参数值转化成DateTime列表。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static List<DateTime> ToDateTimeList(this HttpRequest request, string paramName)
		{
			string[] tempArray = request.ToArray(paramName);
			if (tempArray == null) return new List<DateTime>();
			return tempArray.Select(p => DateTime.Parse(p)).ToList();
		}

		/// <summary>
		/// 将从Request中返回的参数值转化DateTime数组。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static DateTime[] ToDateTimeArray(this HttpRequest request, string paramName)
		{
			return request.ToDateTimeList(paramName).ToArray();
		}

		#endregion

		#region Guid

		/// <summary>
		/// 将从Request中返回的参数值转化成Guid。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static Guid ToGuid(this HttpRequest request, string paramName)
		{
			return request.ToGuid(paramName, Guid.Empty);
		}

		/// <summary>
		/// 将从Request中返回的参数值转化成Guid。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <param name="defaultValue">默认值</param>
		/// <returns></returns>
		public static Guid ToGuid(this HttpRequest request, string paramName, Guid defaultValue)
		{
			string paramValue = request[paramName];
			Guid returnValue;
			if (String.IsNullOrEmpty(paramValue))
			{
				returnValue = defaultValue;
			}
			else
			{
				try
				{
					returnValue = new Guid(paramValue);
				}
				catch (Exception ex)
				{
					returnValue = defaultValue;
					TraceUtil.Write(ex);
				}
			}
			return returnValue;
		}

		/// <summary>
		/// 将从Request中返回的参数值转化成Guid列表。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static List<Guid> ToGuidList(this HttpRequest request, string paramName)
		{
			string[] tempArray = request.ToArray(paramName);
			if (tempArray == null) return new List<Guid>();
			return tempArray.Select(p => new Guid(p)).ToList();
		}

		/// <summary>
		/// 将从Request中返回的参数值转化Guid数组。
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="paramName">参数名。</param>
		/// <returns></returns>
		public static Guid[] ToGuidArray(this HttpRequest request, string paramName)
		{
			return request.ToGuidList(paramName).ToArray();
		}

		#endregion

		#region Pager

		/// <summary>
		/// 获取分页参数
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="pageIndex">当前页索引</param>
		/// <param name="pageSize">每页大小（条数）。</param>
		public static void GetPagerParams(this HttpRequest request, out Int32 pageIndex, out Int32 pageSize)
		{
			request.GetPagerParams(out pageIndex, out pageSize, 20);
		}

		/// <summary>
		/// 获取分页参数
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="pageIndex">当前页索引</param>
		/// <param name="pageSize">每页记录数</param>
		/// <param name="defaultPageSize">默认每页记录数</param>
		public static void GetPagerParams(this HttpRequest request, out int pageIndex, out int pageSize, int defaultPageSize)
		{
			request.GetPagerParams("page", "size", out pageIndex, out pageSize, defaultPageSize);
		}

		/// <summary>
		/// 获取分页参数
		/// </summary>
		/// <param name="request">HttpRequest实例对象。</param>
		/// <param name="qsPageIndex">当前页索引查询字符串</param>
		/// <param name="qsPageSize">每页记录数查询字符串</param>
		/// <param name="pageIndex">当前页索引</param>
		/// <param name="pageSize">每页记录数</param>
		/// <param name="defaultPageSize">默认每页记录数</param>
		public static void GetPagerParams(this HttpRequest request, String qsPageIndex, String qsPageSize, out int pageIndex, out int pageSize, int defaultPageSize)
		{
			if (!Int32.TryParse(request.QueryString[qsPageIndex], out pageIndex) || pageIndex < 1)
			{
				pageIndex = 1;
			}

			if (!Int32.TryParse(request.QueryString[qsPageSize], out pageSize) || pageSize < 1)
			{
				if (defaultPageSize < 1) defaultPageSize = 20;
				pageSize = defaultPageSize;
			}
		}

		#endregion
	}
}
